#include "output.h"

Screen::Screen(Map* m)
{
  initscr();
  raw();
  keypad(stdscr, TRUE);
  noecho();
  curs_set(0);
  start_color();

  refresh();
  mapw = newwin(19, 40, 0, 0);
  statsw = newwin(19, 40, 0, 40);
  messagesw = newwin(5, 80, 19, 0);

  setColors = new bool[NUM_COLORS*NUM_COLORS];
  for(int i = 0; i < NUM_COLORS*NUM_COLORS; i++)
    setColors[i] = false;

  messageList = new string[5];
  for (int i = 0; i < 5; i++)
  {
    messageList[i] = "";
  }

  map = m;

  mapPrint();
  mesPrint();
  statPrint();
}

void Screen::mapPrint()
{
  stringstream s;
  int minx, miny;
  int color;
  if (map->getPosX() < 8)
    minx = 0;
  else if (map->getPosX() - 8 > map->getHeight() - 17)
    minx = map->getHeight() - 17;
  else
    minx = map->getPosX() - 8;

  if (map->getPosY() < 18)
    miny = 0;
  else if (map->getPosY() - 18 > map->getWidth() - 37)
    miny = map->getWidth() - 37;
  else
    miny = map->getPosY() - 18;

  wmove(mapw, 0, 0);
  for(int i = 0; i < 17; i++)
  {
    for(int j = 0; j < 37; j++)
    {
//cout << "Test" << i << "," << j;
      color = mixColor(map->getSpace(minx + i, miny + j)->getFore(), map->getSpace(minx + i, miny + j)->getBack());
      wattron(mapw,COLOR_PAIR(color));
      mvwaddch(mapw,i,j,map->getSpace(minx + i, miny + j)->display());
      wattroff(mapw,COLOR_PAIR(color));
    }
  }
  wrefresh(mapw);
}

void Screen::mesPrint()
{
  int color = mixColor(COLOR_GREEN, COLOR_BLACK);
  wmove(messagesw, 0, 0);
  wattron(messagesw,COLOR_PAIR(color));
  for (int i = 0; i < 5; i++)
  {
    wprintw(messagesw, messageList[i].c_str());
    wprintw(messagesw, "\n");
  }
  wrefresh(messagesw);
  wattroff(messagesw,COLOR_PAIR(color));
}

void Screen::statPrint()
{
  int color = mixColor(COLOR_BLUE, COLOR_BLACK);
  wmove(statsw, 0, 0);
  wattron(statsw,COLOR_PAIR(color));
  wprintw(statsw, "Player:  Example\n");
  wprintw(statsw, "HP:  1000/1000\n");
  wprintw(statsw, "MP:  1000/1000\n");
  wrefresh(statsw);
  wattroff(statsw,COLOR_PAIR(color));
}

void Screen::addMessage(const char* s)
{
  for (int i = 3; i >= 0; i--)
  {
    messageList[i + 1] = messageList[i];
  }
  messageList[0] = s;
}

void Screen::setMap(Map* m)
{
  map = m;
}

int Screen::mixColor(int fore, int back)
{
  if (setColors[fore*NUM_COLORS + back] == false)
  {
    init_pair(fore*NUM_COLORS + back, fore, back);
    setColors[fore*NUM_COLORS + back] = true;
  }

  return fore*NUM_COLORS + back;
}

void Screen::makeColors()
{
  init_color(COLOR_WHITE, 627, 321, 176);
}

void Screen::end()
{
  endwin();
}

Saver::Saver(string file, Map* map)
{
  ofstream out (file.c_str());

  Monster* curMonster;

  out << map->height << endl;
  out << map->width;

  for (int i = 0; i < map->height; i++)
  {
    out << endl;
    for (int j = 0; j < map->width; j++)
    {
      out << map->map[i][j]->displayBase();
    }
  }

  for (int i = 0; i < map->height; i++)
  {
    for (int j = 0; j < map->width; j++)
    {
      if (map->map[i][j]->getPlayer() != NULL)    // FIXME!  i and j shouldn't get output multiple times!
      {
	out << endl;
	out << i << " " << j << " " << map->map[i][j]->getType() << endl << "PLAYER" << endl << "0"  << endl << "==========" << endl;
      }
      else if ((curMonster = map->map[i][j]->getMonster()) != NULL)
      {
	out << endl;
	out << i << " " << j << " " << map->map[i][j]->getType() << endl << curMonster->getName() << endl << curMonster->getID() << endl << "==========" << endl;
      }
      else if (map->map[i][j]->getType() != 0)
      {
	out << endl;
	out << i << " " << j << " " << map->map[i][j]->getType() << endl << "==========" << endl;
      }
    }
  }
  out.close();
}
